package com.lanou.connotationjoke.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lanou.connotationjoke.R;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by dllo on 16/2/25.
 */
public class IntroduceActivity extends BaseActivity{
    private ViewPager viewPager;
    private List<ImageView> images;
    private List<View> dots;
    private ViewPagerAdapter adapter;
    private int[] imageIds = {R.mipmap.bg_intros1, R.mipmap.bg_intros2,
            R.mipmap.bg_intros3, R.mipmap.bg_intros4};
    private int[] dotIds = {R.id.dot_0, R.id.dot_1, R.id.dot_2, R.id.dot_3};
    private SharedPreferences sharePreferences;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Intent intent = new Intent(IntroduceActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharePreferences = getSharedPreferences("introduce", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharePreferences.edit();


        if (sharePreferences.getBoolean("first", true)){
            editor.putBoolean("first",false);
            editor.commit();
            setContentView(R.layout.activity_introduce);
        viewPager = (ViewPager) findViewById(R.id.vp_introduce);
        initData();
        adapter = new ViewPagerAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < 4; i++) {
                    dots.get(i).setBackgroundColor(Color.BLACK);
                }
                dots.get(position).setBackgroundColor(Color.RED);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }else {
            setContentView(R.layout.activity_first);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(0);
                }
            }).start();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }



    private void initData(){
        images = new ArrayList<>();
        dots = new ArrayList<>();
        for (int i = 0; i <4 ; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imageIds[i]);
            images.add(imageView);
            View view = findViewById(dotIds[i]);
            dots.add(view);
        }
        dots.get(0).setBackgroundColor(Color.RED);
        images.get(3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IntroduceActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }


    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return images.size();
        }

        //是否是同一张图片
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            view.removeView(images.get(position));

        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            view.addView(images.get(position));
            return images.get(position);
        }

    }
}
